package com.yy.train.ticket.train.controller;

import java.time.LocalTime;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yy.train.ticket.into.Ticket;
import com.yy.train.ticket.into.TrainNumber;
import com.yy.train.ticket.into.page.TrainNumberPage;
import com.yy.train.ticket.train.entity.TtLineEntity;
import com.yy.train.ticket.utils.LocalDateTimeUtils;
import com.yy.train.ticket.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.yy.train.ticket.train.entity.TtTrainNumberEntity;
import com.yy.train.ticket.train.service.TtTrainNumberService;



/**
 * 车次
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2023-09-26 09:47:24
 */
@RestController
@RequestMapping("tt/number")
public class TtTrainNumberController {
    @Autowired
    private TtTrainNumberService ttTrainNumberService;
    @GetMapping("/list")
    public R list(){
        return R.ok(ttTrainNumberService.list());
    }
    @PostMapping("/ticketlist")
    public R ticketlist(@RequestBody Ticket ticket){
        return ttTrainNumberService.ticketlist(ticket);
    }
    /**
     * 列表
     */
    @PostMapping("/page")
    public R page(@RequestBody TrainNumberPage trainNumberPage){
        LambdaQueryWrapper<TtTrainNumberEntity> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.like(StringUtils.isNotBlank(trainNumberPage.getNumberName()), TtTrainNumberEntity::getNumberName,trainNumberPage.getNumberName());

        Page<TtTrainNumberEntity> pages = new Page<TtTrainNumberEntity>(trainNumberPage.getPageNum(), trainNumberPage.getPageSize());
        IPage<TtTrainNumberEntity> iPage = ttTrainNumberService.page(pages, lambdaQueryWrapper);
        return R.ok(iPage);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{numberId}")
    public R info(@PathVariable("numberId") Long numberId){
		TtTrainNumberEntity ttTrainNumber = ttTrainNumberService.getById(numberId);

        return R.ok(ttTrainNumber);
    }



    /**
     * 保存
     */

    @PostMapping("/save")
    public R save(@RequestBody TrainNumber trainNumber){

        String runTime=  trainNumber.getRunTime();
        LocalTime runTimeLocal= LocalDateTimeUtils.formatterToHH_mm_ss(runTime);
        TtTrainNumberEntity ttTrainNumberEntity=new TtTrainNumberEntity();
        ttTrainNumberEntity.setTrainId(trainNumber.getTrainId());
        ttTrainNumberEntity.setNumberName(trainNumber.getNumberName());
        ttTrainNumberEntity.setLineId(trainNumber.getLineId());
        ttTrainNumberEntity.setNote(trainNumber.getNote());
        ttTrainNumberEntity.setRunTime(runTimeLocal);
        ttTrainNumberService.save(ttTrainNumberEntity);

        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public R update(@RequestBody TrainNumber trainNumber){
        String runTime=  trainNumber.getRunTime();
        LocalTime runTimeLocal= LocalDateTimeUtils.formatterToHH_mm_ss(runTime);
        TtTrainNumberEntity ttTrainNumberEntity=new TtTrainNumberEntity();
        ttTrainNumberEntity.setNumberId(trainNumber.getNumberId());
        ttTrainNumberEntity.setTrainId(trainNumber.getTrainId());
        ttTrainNumberEntity.setNumberName(trainNumber.getNumberName());
        ttTrainNumberEntity.setLineId(trainNumber.getLineId());
        ttTrainNumberEntity.setNote(trainNumber.getNote());
        ttTrainNumberEntity.setRunTime(runTimeLocal);
        ttTrainNumberService.updateById(ttTrainNumberEntity);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public R delete(@RequestBody List<Long>   numberIds){
		ttTrainNumberService.removeByIds(numberIds);

        return R.ok();
    }

}
